package com.example.demo.myStack;

public class IStack {


    private Object[] stack;

    int head = 0;

    int size = 0;

    public IStack(int capacity) {
        this.stack = new Object[capacity];
    }

    public IStack() {
        this.stack = new Object[5];
    }


    public void push(Object o){
        int len = stack.length;
        if (len == size){
            // 说明已经满了，就得扩容
            Object[] newStack = new Object[len+5];
            System.arraycopy(stack,0,newStack,0,size);
            stack = newStack;
        }
        stack[size++] = o;
        head++;
    }

    public Object poll(){
        head--;
        if (head < 0 ){
            return new NullPointerException("栈中没有元素");
        }
        return stack[head];
    }
}


class TestMyStack{

    public static void main(String[] args) {
        IStack stack = new IStack(5);

        for (int i = 0;i<5;i++){
            stack.push(i);
        }

        for (int i =0;i<6;i++){
            System.out.println(stack.poll());
        }
    }
}